/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.weeego.util.image;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 图片缩放
 *
 * @author xuping
 *
 */
public class ImageEdit {

    /**
     * 图片等比缩放
     *
     * @param oldfileurl
     *            旧的文件
     * @param newfileurl
     *            新的文件
     * @param whith
     *            新文件的宽
     * @param hight
     *            新文件的高
     * @throws IOException
     *             异常
     */
    public static void resize_scale(String oldfileurl, String newfileurl,
            int whith, int hight) throws IOException {
        BufferedImage srcImage;
        File fromFile = new File(oldfileurl);
        srcImage = ImageIO.read(fromFile);
        if (srcImage.getWidth() > whith || srcImage.getHeight() > hight) {

            if (srcImage.getWidth() / whith > srcImage.getHeight() / hight) {

                resize(oldfileurl, newfileurl, whith, whith * srcImage.getHeight() / srcImage.getWidth());
            } else {
                resize(oldfileurl, newfileurl, hight * srcImage.getWidth() / srcImage.getHeight(), hight);
            }
        } else {
            resize(oldfileurl, newfileurl, srcImage.getWidth(), srcImage.getHeight());
        }

    }

    /**
     * 图片缩放
     *
     * @param oldfileurl
     *            旧文件的地址
     * @param newfileurl
     *            新文件的地址
     * @param whith
     *            生成文件的宽
     * @param hight
     *            生成文件的高
     */
    public static void resize(String oldfileurl, String newfileurl, int whith,
            int hight) {
        java.io.File file = new java.io.File(oldfileurl);
        Image src;

        try {
            src = javax.imageio.ImageIO.read(file);
            int new_w = 0;
            int new_h = 0;
            new_w = whith;
            new_h = hight;
            BufferedImage tag = new BufferedImage(new_w, new_h,
                    BufferedImage.TYPE_INT_RGB);
            tag.getGraphics().drawImage(src, 0, 0, new_w, new_h, null);
            String newfileurlPath = newfileurl.substring(0, newfileurl.lastIndexOf("/") + 1);
            CreateFolder(newfileurlPath);//创建目录
            FileOutputStream newimage = new FileOutputStream(newfileurl);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
            encoder.encode(tag);
            newimage.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }

    /**
     *  建立目录，给定目录路径，一层层判断该目录是否存在，不存在则建立，再判断下一目录
     * @param sPath 要创建的目录
     * @return 建立成功返回空，否则返回错误信息
     */
    private static String CreateFolder(String sPath) {
        String sErr = "";
        String sTmpPath;
        String[] aP;
        sTmpPath = sPath;

        sTmpPath = sTmpPath.replaceAll("\\\\", "/");
        if (sTmpPath.indexOf("/") == -1) {
            sErr = "路径错误！";
        } else {
            File f = null;
            aP = sPath.split("/");
            try {
                for (int i = 0; i < aP.length; i++) {
                    sTmpPath = "";
                    for (int j = 0; j <= i; j++) {
                        sTmpPath += aP[j] + "/";
                    }
                    f = new File(sTmpPath);
                    if (!f.exists()) {
                        f.mkdir();
                    }
                }
            } catch (Exception e) {
                sErr = e.getMessage();
            }
        }
        return sErr;
    }

    public static void main(String[] args) {
        ImageEdit _ImageEdit = new ImageEdit();
        _ImageEdit.resize("i:/e/t/temp_1.jpg", "i:/e/t/111.jpg", 160, 110);
    }
}
